package com.example.hao.flowercityfm.HFAdapter.ViewHolder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.hao.flowercityfm.R;
import com.example.hao.flowercityfm.application.MyApp;
import com.example.hao.flowercityfm.pojo.HomeFragmentData;
import com.example.hao.flowercityfm.pojo.Type4ListItem;
import com.example.hao.flowercityfm.utils.HttpUtils;
import com.google.gson.Gson;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Type4ViewHolder extends BaseTypeViewHolder {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;
    private Type4Adapter type4Adapter;

    public Type4ViewHolder(Context context, View itemView) {
        super(context, itemView);
        initView();
    }

    public void initView() {
        rv.setLayoutManager(new GridLayoutManager(context, 3));
        type4Adapter = new Type4Adapter(context);
        rv.setAdapter(type4Adapter);
        rv.addItemDecoration(new ItemDecoration(context.getResources().getDimensionPixelOffset(R.dimen._5dp)));


    }

    private static final String URL_4 = "https://api.myjson.com/bins/xbdyg";
    private static final String URL_5 = "https://api.myjson.com/bins/t2l6o";
    private static final String URL_6 = "https://api.myjson.com/bins/1dywio";
    private static final String URL_7 = "https://api.myjson.com/bins/ksnvk";
    private static final String URL_8 = "https://api.myjson.com/bins/xw76o";
    private static final String URL_9 = "https://api.myjson.com/bins/14fyu8";
    private static final String URL_10 = "https://api.myjson.com/bins/1azqhs";
    private static final String URL_11 = "https://api.myjson.com/bins/bxiog";
    private static final String URL_12 = "https://api.myjson.com/bins/p11zk";
    private static final String URL_13 = "https://api.myjson.com/bins/w698w";
    private static final String URL_14 = "https://api.myjson.com/bins/8ycnk";
    private static final String URL_15 = "https://api.myjson.com/bins/n8r68";

    private static final String URL_16 = "https://api.myjson.com/bins/1ftk7k";


    private static String address = null;

    private void loadData() {

        HttpUtils.method2(address, new HttpUtils.OnOkCallBack() {
            @Override
            public void onFailure(@NonNull IOException e) {
                Log.e(MyApp.TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(@NonNull String jsonStr) {

                Type4ListItem type4ListItem = new Gson().fromJson(jsonStr, Type4ListItem.class);
                type4Adapter.setItem(type4ListItem);
            }
        });
    }

    public class Type4Adapter extends RecyclerView.Adapter<Type4Adapter.ViewHolder> {


        private Context context;

        private Type4Adapter(Context context) {
            this.context = context;
        }

        private Type4ListItem item;

        private HomeFragmentData.ListBean listBean;

        public void setItem(Type4ListItem item) {
            this.item = item;
            notifyDataSetChanged();
        }


        @Override
        public int getItemViewType(int position) {

            return super.getItemViewType(position);
        }

        @NonNull
        @Override
        public Type4Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_home_type3_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull Type4Adapter.ViewHolder holder, int position) {

            Type4ListItem.DataBean.PageListBean pageListBean = item.getData().getPageList().get(position);
            holder.tvInformation.setText(pageListBean.getIntro());
            holder.tvName.setText(pageListBean.getName());
            Glide.with(context).load(pageListBean.getPictureUrl()).into(holder.ivBg);
        }

        @Override
        public int getItemCount() {

            return item == null ? 0 : item.getData().getPageList().size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.iv_bg)
            ImageView ivBg;
            @BindView(R.id.tv_name)
            TextView tvName;
            @BindView(R.id.tv_information)
            TextView tvInformation;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

    }


    private String[] titles = new String[]{"新鲜资讯", "有声书", "声音广州", "学习之声", "金融理财", "教育", "情感",
            "医疗健康", "体育", "脱口秀", "区区有精彩", "精品节目", "音乐故事", "主持人", "为你推荐"};

    @Override
    public void setData(HomeFragmentData.ListBean listBean) {
        int item = listBean.getItem();
        switch (item) {
            case 4:
                title.setText(titles[0]);
                address = URL_4;
                break;
            case 5:
                title.setText(titles[1]);
                address = URL_5;
                break;
            case 6:
                title.setText(titles[2]);
                address = URL_6;
                break;
            case 7:
                title.setText(titles[3]);
                address = URL_7;
                break;
            case 8:
                title.setText(titles[4]);
                address = URL_8;
                break;
            case 9:
                title.setText(titles[5]);
                address = URL_9;
                break;
            case 10:
                title.setText(titles[6]);
                address = URL_10;
                break;
            case 11:
                title.setText(titles[7]);
                address = URL_11;
                break;
            case 12:
                title.setText(titles[8]);
                address = URL_12;
                break;
            case 13:
                title.setText(titles[9]);
                address = URL_13;
                break;
            case 14:
                title.setText(titles[10]);
                address = URL_14;
                break;
            case 15:
                title.setText(titles[11]);
                address = URL_15;
                break;
            case 16:
                title.setText(titles[12]);
                address = URL_16;
                break;
            case 17:
                title.setText(titles[13]);
                break;
            case 18:
                title.setText(titles[14]);
                break;
            case 19:
                title.setText(titles[15]);
                break;
        }
        loadData();
    }

}
